﻿/*
 * CodeClean for Visual Studio. A program to manage Visual C++ code.
 * Copyright (C) 2012 Gordon Kristan
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using EnvDTE;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.TextManager.Interop;
using Microsoft.VisualStudio.VCCodeModel;
using Microsoft.Win32;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System;

namespace GordonKristan.codeclean
{
    [PackageRegistration(UseManagedResourcesOnly = true)]
    // Registry key
    [DefaultRegistryRoot("Software\\CodeClean")]
    // Help/about screen information.
    [InstalledProductRegistration(false, "#110", "#112", "1.0", IconResourceID = 400)]
    // PLK. Need to request from Microsoft when ready.
    [ProvideLoadKey("Standard", "1.0", "CodeClean", "Gordon Kristan", 1)]
    // This attribute is needed to let the shell know that this package exposes some menus.
    [ProvideMenuResource(1000, 1)]
    [Guid(GuidList.guidCodeCleanPkgString)]
	//Inserts our dialogs into the option menu in Visual Studio
	[ProvideOptionPage(typeof(OptionsPageStyle), "CodeClean", "Style", 0, 0, true)] 
	//Give a tool window to this package
	[ProvideToolWindow(typeof(ToolWindow))]
	//Main package class for the extensions
    public sealed class codecleanPackage : Package
    {
		private CodeCleanInterface cci;

        /// <summary>
        /// Default constructor of the package.
		/// Most Visual Studio services aren't loaded at this point,
		/// so it's best to do most of the work in the initializer.
        /// </summary>
        public codecleanPackage()
        {

		}

        /// <summary>
        /// All initialization code should go here.
        /// </summary>
        protected override void Initialize()
        {
            base.Initialize();
			cci = new CodeCleanInterface();

            // Add our command handlers for menu (commands must exist in the .vsct file)
            OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
            if ( null != mcs )
            {
				CommandID menuCommandID;
				MenuCommand menuItem;
				// Style File command
				menuCommandID = new CommandID(GuidList.guidCodeCleanCmdSet, (int)PkgCmdIDList.styleFileCommand);
				menuItem = new MenuCommand(styleFileCallback, menuCommandID);
                mcs.AddCommand( menuItem );
				// Configuration command
				menuCommandID = new CommandID(GuidList.guidCodeCleanCmdSet, (int)PkgCmdIDList.settingsCommand);
				menuItem = new MenuCommand(configurationCallback, menuCommandID);
				mcs.AddCommand(menuItem);
				// Function Manager command
				menuCommandID = new CommandID(GuidList.guidCodeCleanCmdSet, (int)PkgCmdIDList.functionManagerCommand);
				menuItem = new MenuCommand(functionManagerCallback, menuCommandID);
				mcs.AddCommand(menuItem);
				// Comment Manager Command
				menuCommandID = new CommandID(GuidList.guidCodeCleanCmdSet, (int)PkgCmdIDList.commentManagerCommand);
				menuItem = new MenuCommand(commentManagerCallback, menuCommandID);
				mcs.AddCommand(menuItem);
				// Comment Manager Command
				menuCommandID = new CommandID(GuidList.guidCodeCleanCmdSet, (int)PkgCmdIDList.alphabetizeCommand);
				menuItem = new MenuCommand(alphabetizeCallback, menuCommandID);
				mcs.AddCommand(menuItem);
				// Tool Window Command
				menuCommandID = new CommandID(GuidList.guidCodeCleanCmdSet, (int)PkgCmdIDList.toolWindowCommand);
				menuItem = new MenuCommand(ShowToolWindow, menuCommandID);
				mcs.AddCommand(menuItem);
            }
        }

		/// <summary>
		/// Called when the user chooses to show the tool window.
		/// </summary>
		private void ShowToolWindow(object sender, EventArgs e)
		{
			// Show the tool window. The last param creates it if it doesn't exits.
			ToolWindowPane window = this.FindToolWindow(typeof(ToolWindow), 0, true);
			if ((null == window) || (null == window.Frame))
			{
				throw new NotSupportedException("Could not create tool window.");
			}
			IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
			Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(windowFrame.Show());
		}

        /// <summary>
        /// Called when the user clicks the Style File button
        /// </summary>
        private void styleFileCallback(object sender, EventArgs e)
        {
			cci.styleFile();
        }

		/// <summary>
		/// Called when the user clicks the Configuration button
		/// </summary>
		private void configurationCallback(object sender, EventArgs e)
		{

		}

		/// <summary>
		/// Called when the user clicks the Function Manager button
		/// </summary>
		private void functionManagerCallback(object sender, EventArgs e)
		{

		}

		/// <summary>
		/// Called when the user clicks the Comment Manager button
		/// </summary>
		private void commentManagerCallback(object sender, EventArgs e)
		{
			cci.commentManager();
		}

		/// <summary>
		/// Called when the user clicks the Alphabetize Selected Lines button
		/// </summary>
		private void alphabetizeCallback(object sender, EventArgs e)
		{
			cci.alphabetizeLines();
		}


    }
}